<?php

	// +----------------------------------------------------------------------
	// | 店铺配置
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\controller\store\admin;

	use app\common\event\Setting as SettingEvent;
	use app\common\model\SystemConfig as SystemConfigModel;
	use think\facade\Config;
	use yicmf\builder\Edit;
	use app\pay\model\UserBalanceCash as UserBalanceCashModel;
	use app\pay\event\UserBalanceAccount as UserBalanceAccountEvent;
	use app\common\event\SystemConfig as SystemConfigEvent;
	use app\model\store\Template as TemplateModel;

	class Setting extends Common
	{
		/**
		 * 商品设置
		 * @return string
		 */
		public function index()
		{
			if ($this->request->isPost()) {
				$event = new SystemConfigEvent();
				$re = $event->updateValue($this->request->post(),$this->user);
				$this->_returnAuto($re,false);
			} else {
				$set = SystemConfigModel::where('status', 1)->where('module', 'store')->order('sort ASC')->select();
				$builder = new Edit();
				$builder->title('配置管理');

				$data = [];
				$group = [];
				foreach ($set as $item) {
					$data[$item['group']][$item['name']] = $item['value'];
					$group[$item['group_title']][] = $item['group'].'.'.$item['name'];
					if ($item['type'] == 'input') {
						$builder->keyTextInline($item['group'].'.'.$item['name'], $item['title'], $item['tips']);
					} elseif ('switch' == $item['type']) {
						$builder->keySwitch($item['group'].'.'.$item['name'], $item['title'], $item['tips']);
					} elseif ('textarea' == $item['type']) {
						$builder->keyTextArea($item['group'].'.'.$item['name'], $item['title'], $item['tips']);
					} elseif ('number' == $item['type']) {
						$builder->keyNumber($item['group'].'.'.$item['name'], $item['title'], $item['tips']);
					}
				}
				foreach ($group as $key => $item) {
					$builder->group($key, $item);
				}
//					->explain('请填写真实调用的控制器及方法，无需注意路由配置')
				$builder->data($data)
					->buttonSubmit();
				return $builder->fetch();
			}
		}

		/**
		 * 收款设置
		 * @return mixed
		 * @throws \Exception
		 * @author  : 微尘 <yicmf@qq.com>
		 * @datetime: 2019/3/15 11:03
		 */
		public function settlement()
		{
			if ($this->request->isPost()) {
				$event = new UserBalanceAccountEvent();
				$result = $event->update($this->request->param(), $this->user);
				if (0 == $result['code']) {
					$this->_returnSuccess($result['message']);
				} else {
					$this->_returnError($result['message']);
				}
			} else {
				$account = $this->user->accounts()->where('is_default', 1)->find() ?: [];
				$builder = new Edit();
				$builder->title('收款方式')
					->keyHidden('id')
					->keySelect('channel', '收款方式', UserBalanceCashModel::$channel)
					->keyTextInline('account', '提现账号', '填写支付宝账号')
					->keyTextInline('real_name', '收款人姓名', '收款人的真实姓名')
					->keyImageModel('qrcode_id', '收款二维码', '')
					->data($account)
					->explain('建议使用支付宝方式，自动提现同时可以自动提现')
					->buttonSubmit('', '保 存');
				return $builder->fetch();
			}
		}

		/**
		 * 授权
		 * @return mixed
		 * @author  : 微尘 <yicmf@qq.com>
		 * @datetime: 2019/3/15 11:04
		 */
		public function authorization()
		{
			$system_config = [];
			$system_config['os'] = php_uname('s'); // 服务器操作系统
			$system_config['server_software'] = $_SERVER['SERVER_SOFTWARE']; // 服务器环境
			$system_config['upload_max_filesize'] = @ini_get('file_uploads') ? ini_get('upload_max_filesize') : 'unknow'; // 文件上传限制
			$system_config['gd_version'] = gd_info()['GD Version']; // GD（图形处理）版本
			$system_config['max_execution_time'] = ini_get("max_execution_time") . "秒"; // 最大执行时间
			$system_config['port'] = $_SERVER['SERVER_PORT']; // 端口
			$system_config['dns'] = $_SERVER['HTTP_HOST']; // 服务器域名
			$system_config['php_version'] = PHP_VERSION; // php版本
			$system_config['ip'] = $_SERVER['SERVER_ADDR']; // 服务器
			$system_config['sockets'] = extension_loaded('sockets'); //是否支付sockets
			$system_config['openssl'] = extension_loaded('openssl'); //是否支付openssl
			$system_config['curl'] = function_exists('curl_init'); // 是否支持curl功能
			$system_config['fileinfo'] = function_exists('finfo_open'); // 是否支持curl功能
			$this->assign('system_config', $system_config);
			return $this->fetch();
		}
	}
